<?php
class Lion_View_Helper_Translate extends Zend_Controller_Action_Helper_Abstract
{
    public function translate ($label, $section)
    {
        $sess = new Zend_Session_Namespace('locale');
        $curr_lang = $sess->current_lang;
        $registry = Zend_Registry::getInstance();
        $this->db = $registry->get('db');
        $locale = $curr_lang;
        if($curr_lang != '')
        {
            $lang = $curr_lang;
        } else {
    	    $lang = 'en';
	}
	    
    
	    $this->db->query("SET NAMES 'utf8'");
	    $sql1 = $this->db->fetchAll("
	    	SELECT id 
	    	FROM td_labels_key
	    	WHERE keyText = '" . stripslashes($label) ."' ");
	    if(count($sql1) == 0)
	    {	        
	    	$add_labels = $this->db->query("
			      INSERT
			      INTO td_labels_key(keyText)
			      VALUES ('" . addslashes($label) . "')");
	        $last_insert_id = $this->db->lastInsertId();
	        if ($add_labels) {
	            
	        	$label = str_replace('%', '', $label);
	        	$add_labels_key = $this->db->query("
			        INSERT
			        INTO td_labels
			        VALUES(" . $last_insert_id . ", '" . addslashes($label) . "', '" . addslashes($label) . "', '" . $section . "')");
	        }	
	    } else {    
	        foreach ($sql1 as $result1)
	        {
	            $id = $result1['id'];
	            $this->db->query("SET NAMES 'utf8'");
	            $sql2 = $this->db->fetchAll("
	                SELECT en_text, ru_text
	                FROM td_labels
	                WHERE id = '" . $id . "' 
	            ");
	            
	            foreach ($sql2 as $result2)
	            {
	                if($lang == "en")
	                {
	                    $contents = $result2['en_text'];
	                } else if($lang == "ru")
	                {
	                    $contents = $result2['ru_text'];
	                }
	                $contents = str_replace('%', '', $contents);  	
	        	    return str_replace('<p>', '', $contents);
	            }	            
	        }
	    }     
  	}
}
